function getYCGSSearchUrl(freeSearchOp, matchingAllTerms, matchingSearchHash) {
    return "/addama-rest/dudley-sample/path/datasource/YCGS/search?" + freeSearchOp + matchingAllTerms +
           "&PROJECTION=YCGSName" +
           "&PROJECTION=YCGSDudleyProject" +
           "&PROJECTION=YCGSSeqRun" +
           "&PROJECTION=YCGSStrain" +
           "&PROJECTION=YCGSFlowcell" +
           "&PROJECTION=YCGSLane" +
           "&PROJECTION=YCGSHasBam" +
           "&PROJECTION=YCGSBam" +
           "&PROJECTION=YCGSBuild" +
           "&PROJECTION=YCGSComments" +
           "&PROJECTION=YCGSMethods" +
           "&PROJECTION=YCGSCoverage" +
           "&PROJECTION=YCGSPercentage" +
           //"&PROJECTION=IGV" +
           matchingSearchHash;
}

function getYCGSPagingColumns() {
    return [
        {
            name: 'indexNum',
            type: 'float'
        },
        {
            name: 'key'
        },
        {
            name: 'YCGSName'
        },
        {
            name: 'YCGSDudleyProject'
        },
        {
            name: 'YCGSSeqRun'
        },
        {
            name: 'YCGSStrain'
        },
        {
            name: 'YCGSFlowcell'
        },
        {
            name: 'YCGSLane'
        },
        {
            name: 'YCGSHasBam'
        },
        {
            name: 'YCGSBam'
        },
        {
            name: 'YCGSComments'
        },
        /*{
            name: 'IGV'
        },*/
        {
            name: 'YCGSBuild'
        },
        {
            name: 'YCGSCoverage'
        },
        {
            name: 'YCGSPercentage'
        },
        {
            name: 'YCGSMethods'
        }
    ];

}

function translateYCGSResultToJson(jsonSearchArray) {
    var jsonObj = [];
    var cgsKey = jsonSearchArray.name;
    jsonObj[0] = cgsKey.substring(4, cgsKey.length);
    jsonObj[1] = jsonSearchArray.name;
    jsonObj[2] = jsonSearchArray.YCGSName;
    jsonObj[3] = jsonSearchArray.YCGSDudleyProject;
    jsonObj[4] = jsonSearchArray.YCGSSeqRun;
    jsonObj[5] = jsonSearchArray.YCGSStrain;
    jsonObj[6] = jsonSearchArray.YCGSFlowcell;
    jsonObj[7] = jsonSearchArray.YCGSLane;
    jsonObj[8] = jsonSearchArray.YCGSHasBam;
    jsonObj[9] = jsonSearchArray.YCGSBam;
    jsonObj[10] = jsonSearchArray.YCGSComments;
    //jsonObj[11] = (jsonSearchArray.IGV != null) ? jsonSearchArray.IGV : "off";
    jsonObj[11] = jsonSearchArray.YCGSBuild;
    jsonObj[12] = jsonSearchArray.YCGSCoverage;
    jsonObj[13] = jsonSearchArray.YCGSPercentage;
    jsonObj[14] = jsonSearchArray.YCGSMethods;
    return jsonObj;
}

function initYCGSJson(cgshash) {
    cgshash.YCGSName = undefined;
}

function getYCGSSearchOptionsArray() {
    return [
        ['all', 'All'],
        ['YCGSKey', 'YCGSKey'],
        ['YCGSSeqRun', 'YCGSSeqRun'],
        ['YCGSStrain', 'YCGSStrain']
    ];
}

function getYCGSSearchColumnHeaders() {
    return  [
        {
            header: 'Index',
            width: 50,
            sortable: true,
            dataIndex: 'indexNum'
        },
        {
            id:'YCGSKey',
            header: 'YCGSKey',
            minColumnWidth: 100,
            sortable: true,
            dataIndex: 'key'
        },
        {
            header: 'Project',
            width: 150,
            sortable: true,
            dataIndex: 'YCGSDudleyProject'
        },
        {
            header: 'SeqJob',
            width: 100,
            sortable: true,
            dataIndex: 'YCGSSeqRun'
        },
        {
            header: 'Strain',
            width: 100,
            sortable: true,
            dataIndex: 'YCGSStrain'
        },
        {
            header: 'Flowcell',
            width: 125,
            sortable: true,
            dataIndex: 'YCGSFlowcell'
        },
        {
            header: 'Lane',
            width: 50,
            sortable: true,
            dataIndex: 'YCGSLane'
        }/*,
        {
            header: 'IGV',
            width: 50,
            sortable: true,
            dataIndex: 'IGV'
        }*/
    ];
}

function populateYCGSJson(oHash, g) {
    oHash.name = g.getSelectionModel().getSelected().data.key;
    oHash.project = g.getSelectionModel().getSelected().data.YCGSDudleyProject;
    oHash.YCGSSeqRun = g.getSelectionModel().getSelected().data.YCGSSeqRun;
    oHash.YCGSStrain = g.getSelectionModel().getSelected().data.YCGSStrain;
    oHash.YCGSFlowcell = g.getSelectionModel().getSelected().data.YCGSFlowcell;
    oHash.YCGSLane = g.getSelectionModel().getSelected().data.YCGSLane;
    oHash.YCGSHasBam = g.getSelectionModel().getSelected().data.YCGSHasBam;
    oHash.YCGSBam = g.getSelectionModel().getSelected().data.YCGSBam;
    oHash.YCGSComments = g.getSelectionModel().getSelected().data.YCGSComments;
    //oHash.IGV = g.getSelectionModel().getSelected().data.IGV;
    oHash.YCGSBuild = g.getSelectionModel().getSelected().data.YCGSBuild;
    oHash.YCGSCoverage = g.getSelectionModel().getSelected().data.YCGSCoverage;
    oHash.YCGSPercentage = g.getSelectionModel().getSelected().data.YCGSPercentage;
    oHash.YCGSMethods = g.getSelectionModel().getSelected().data.YCGSMethods;

}

function validateCreateUpdateYCGS() {

    return true;
}

function getDudleyProjectCombo(datasource, hashObj) {
    var dudleyProjectData = [
        ['19', 'Dudley Natural Variation'],
        ['33', 'Dudley NV Multiplex RAD'],
        ['30', 'Dudley NV Paired End'],
        ['6',  'Dudley RM-11'],
        ['15', 'Dudley RNA Detection']
    ];
    var dudleyProjectStore = new Ext.data.SimpleStore({
        fields: ['key', 'value'],
        data : dudleyProjectData
    });

    return new Ext.form.ComboBox({
        store: dudleyProjectStore,
        id: datasource + 'DudleyProject',
        fieldLabel: 'Project',
        displayField:'value',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:hashObj.project,
        forceSelection:true,
        selectOnFocus:true
    });
}

function populateSingleYCGSViaSearch(strainHash, rec) {

    strainHash.name = rec[1];
    strainHash.YCGSName = rec[2];
    strainHash.project = rec[3];
    strainHash.YCGSSeqRun = rec[4];
    strainHash.YCGSStrain = rec[5];
    strainHash.YCGSFlowcell = rec[6];
    strainHash.YCGSLane = rec[7];
    strainHash.YCGSHasBam = rec[8];
    strainHash.YCGSBam = rec[9];
    strainHash.YCGSComments = rec[10];
    //strainHash.IGV = rec[11];
    strainHash.YCGSBuild = rec[11];
    strainHash.YCGSCoverage = rec[12];
    strainHash.YCGSPercentage = rec[13];
    strainHash.YCGSMethods = rec[14];

}

var coverageSummary = [
    {
        name:'chrI',
        values:[80,109,200]
    },
    {
        name:'chrII',
        values:[96,49,100]
    },
    {
        name:'chrIII',
        values:[92,52,100],
        labels:['pct_base_called','cvg_of_base_called','chr_copy']
    },
    {
        name:'chrIV',
        values:[93,48,100],
        labels:['pct_base_called','cvg_of_base_called','chr_copy']
    },
    {
        name:'chrV',
        values:[95,50,100],
        labels:['pct_base_called','cvg_of_base_called','chr_copy']
    },
    {
        name:'chrVI',
        values:[93,52,100],
        labels:['pct_base_called','cvg_of_base_called','chr_copy']
    },
    {
        name:'chrVII',
        values:[95,50,100],
        labels:['pct_base_called','cvg_of_base_called','chr_copy']
    },
    {
        name:'chrVIII',
        values:[95,50,100],
        labels:['pct_base_called','cvg_of_base_called','chr_copy']
    },
    {
        name:'chrIX',
        values:[95,50,100],
        labels:['pct_base_called','cvg_of_base_called','chr_copy']
    },
    {
        name:'chrX',
        values:[95,50,100],
        labels:['pct_base_called','cvg_of_base_called','chr_copy']
    },
    {
        name:'chrXI',
        values:[95,50,100],
        labels:['pct_base_called','cvg_of_base_called','chr_copy']
    },
    {
        name:'chrXII',
        values:[95,50,100],
        labels:['pct_base_called','cvg_of_base_called','chr_copy']
    },
    {
        name:'chrXIII',
        values:[95,50,100],
        labels:['pct_base_called','cvg_of_base_called','chr_copy']
    },
    {
        name:'chrXVI',
        values:[95,50,100],
        labels:['pct_base_called','cvg_of_base_called','chr_copy']
    },
    {
        name:'chrXV',
        values:[95,50,100],
        labels:['pct_base_called','cvg_of_base_called','chr_copy']
    },
    {
        name:'chrXVI',
        values:[95,50,100],
        labels:['pct_base_called','cvg_of_base_called','chr_copy']
    },
    {
        name:'2micron',
        values:[0,0,0],
        labels:['pct_base_called','cvg_of_base_called','chr_copy']
    },
    {
        name:'chrM',
        values:[79,120,200],
        labels:['pct_base_called','cvg_of_base_called','chr_copy']
    }
];

function setCoverageSummary(myjson) {
    coverageSummary = myjson;
}

function getCoverageSummary() {
    return coverageSummary;
}

var percentageSummary = {};

function setPercentageSummary(myjson) {
    coveragePercentage = myjson;
}

function getPercentageSummary() {
    return percentageSummary;
}

var n = 18, m = 2, data = pv.range(n).map(function() {
    return pv.range(m).map(function() {
        return Math.random() + .1;
    });
});

var chrnames = ['chrI', 'chrII', 'chrIII', 'chrIV', 'chrV', 'chrVI','chrVII','chrVIII','chrIX','chrX','chrXI','chrXII','chrXIII','chrIV','chrV','chrVI','2micron','chrM'];

var chrvalues = [];

function setCoverageData(coverageArray, percentageArray) {
    for (var i = 0; i < coverageArray.length; i++) {
        var rr = [];
        rr[0] = coverageArray[i];
        rr[1] = percentageArray[i];
        chrvalues[i] = rr;
    }
}

/*
The summary statistics will be gathered from the url parameters from parent window
 */
function getCoverageData() {
    var parameters = getUrlVars();
    var percentageArray = JSON.parse(parameters["percentage"]);
    var coverageArray = JSON.parse(parameters["coverage"]);
    for (var i = 0; i < coverageArray.length; i++) {
        var rr = [];
        rr[0] = coverageArray[i];
        rr[1] = percentageArray[i];
        chrvalues[i] = rr;
    }
    return chrvalues;
}

function getCGSName() {
    var parameters = getUrlVars();
    return parameters["ycgskey"];
}

